package com.cms.plugin.hook.listener;

import com.cms.common.hook.vo.HookPluginObject;
import com.cms.plugin.hook.HookServices;
import com.gitee.starblues.integration.listener.PluginInitializerListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class HookPluginInitializerListener implements PluginInitializerListener {

    @Override
    public void before() {
        // Log the start of plugin initialization
        log.info("plugin init before");
    }


    @Override
    public void complete() {
        // Get the iterator of the hookMap
        Iterator<Map.Entry<String, List<HookPluginObject>>> iterator = HookServices.hookMap.entrySet().iterator();
        while (iterator.hasNext()) {
            // Get the next entry in the hookMap
            Map.Entry<String, List<HookPluginObject>> entry = iterator.next();
            // Get the list of HookPluginObject
            List<HookPluginObject> hookObjects = entry.getValue();
            // Sort the list of HookPluginObject by sortBy
            hookObjects.sort(Comparator.comparingInt(HookPluginObject::getSortBy));
        }

        // Log the completion of plugin initialization
        log.info("init completed");
    }

    @Override
    public void failure(Throwable throwable) {
        // Log the failure of plugin initialization
        log.info("init fail:" + throwable.getMessage());
    }
}